''' <summary>
''' This is a game component that implements IUpdateable.
''' </summary>
Public Class overlay
    Inherits Microsoft.Xna.Framework.DrawableGameComponent
    Private spriteBatch As SpriteBatch
    Private mFont As SpriteFont
    ' Buttons
    Private mLRectangle As Rectangle
    Private mRRectangle As Rectangle
    Private mMRectangle As Rectangle
    Private mJumpRectangle As Rectangle
    Private bmpLeft As Texture2D
    Private bmpRight As Texture2D
    Private bmpNothing As Texture2D
    Private bmpJump As Texture2D
    ' Frame Stuff
    Private mblnDisplayFrameRate As Boolean = True
    Private mintFrameRate As Integer = 0
    Private mintFrameCounter As Integer = 0
    Private mtsElapsedTime As TimeSpan = TimeSpan.Zero
    Private mtmrStartTime As TimeSpan
    Private mtmrGameTime As GameTime
    Private mtmrPauseTime As TimeSpan = TimeSpan.Zero
    Private mtmrPauseTimeAcc As TimeSpan = TimeSpan.Zero
    Private mGameRun As IGameProto
    Private mTime As Single
    Public Event TimeEnded()
    Public Event ClickedItem(ByVal Type As Integer)
    Public Property Score As Long = 0
    Public Property Jumps As Integer = 0
    Public Property StartTime As Boolean = False
    Public Property Time As Single
        Get
            Dim reduceTime As TimeSpan = mtmrPauseTimeAcc
            If (mtmrPauseTime <> TimeSpan.Zero) Then
                reduceTime += mtmrGameTime.TotalGameTime - mtmrPauseTime
            End If
            Dim EndTime As Single = CSng(((mtmrStartTime.Ticks + reduceTime.Ticks) + TimeSpan.TicksPerMillisecond * mTime * 1000.0 - mtmrGameTime.TotalGameTime.Ticks) / (TimeSpan.TicksPerMillisecond * 1000.0))
            If (EndTime < 0) Then
                EndTime = 0
                RaiseEvent TimeEnded()
            End If
            Return EndTime
        End Get
        Set(value As Single)
            mtmrPauseTime = TimeSpan.Zero
            mtmrPauseTimeAcc = TimeSpan.Zero
            If (Not mtmrGameTime Is Nothing) Then
                mtmrStartTime = mtmrGameTime.TotalGameTime
            End If
            mTime = value
        End Set
    End Property
    Public Sub New(ByVal game As Game, ByVal GameRun As IGameProto)
        MyBase.New(game)
        mGameRun = GameRun
        ' TODO: Construct any child components here
    End Sub

    ''' <summary>
    ''' Allows the game component to perform any initialization it needs to before starting
    ''' to run.  This is where it can query for any required services and load content.
    ''' </summary>
    Public Overrides Sub Initialize()
        ' TODO: Add your initialization code here
        MyBase.Initialize()
    End Sub
    Public Overrides Sub Draw(gameTime As Microsoft.Xna.Framework.GameTime)
        Dim States As RenderStates = New RenderStates
        RecordRenderState(States, GraphicsDevice)
        spriteBatch.Begin()
        If (mLRectangle = Rectangle.Empty) Then
            mRRectangle = New Rectangle(GraphicsDevice.Viewport.Width - bmpRight.Width, GraphicsDevice.Viewport.Height - bmpRight.Height - 40, bmpRight.Width, bmpRight.Height)
            mMRectangle = New Rectangle(GraphicsDevice.Viewport.Width - bmpRight.Width - bmpNothing.Width, GraphicsDevice.Viewport.Height - bmpNothing.Height - 40, bmpNothing.Width, bmpNothing.Height)
            mLRectangle = New Rectangle(GraphicsDevice.Viewport.Width - bmpRight.Width - bmpNothing.Width - bmpLeft.Width, GraphicsDevice.Viewport.Height - bmpLeft.Height - 40, bmpLeft.Width, bmpLeft.Height)
            mJumpRectangle = New Rectangle(20, GraphicsDevice.Viewport.Height - bmpJump.Height - 40, bmpJump.Width, bmpJump.Height)
        End If
        ' 
        ' Buttons overlays
        Dim colour As Color = New Color(150, 150, 150, 200)
        spriteBatch.Draw(bmpRight, mRRectangle, colour)
        spriteBatch.Draw(bmpNothing, mMRectangle, colour)
        spriteBatch.Draw(bmpLeft, mLRectangle, colour)
        spriteBatch.Draw(bmpJump, mJumpRectangle, colour)
        ' Score
        DrawWord("Score : " + Me.Score.ToString, 5, 5, Color.White)
        ' FPS
        If (mblnDisplayFrameRate) Then
            mintFrameCounter += 1
            DrawWord("FPS : " + mintFrameRate.ToString, GraphicsDevice.Viewport.Width - 5, GraphicsDevice.Viewport.Height - 40, Color.White, 2)
        End If
        ' Time
        If (mtmrStartTime.Ticks = 0) Then
            mtmrStartTime = gameTime.TotalGameTime
        End If
        If (Me.StartTime) Then
            If (mtmrPauseTime <> TimeSpan.Zero) Then
                mtmrPauseTimeAcc += gameTime.TotalGameTime - mtmrPauseTime
            End If
            mtmrPauseTime = TimeSpan.Zero
        Else
            If (mtmrPauseTime = TimeSpan.Zero) Then
                mtmrPauseTime = gameTime.TotalGameTime
            End If
        End If


        DrawWord("Time : " + Me.Time.ToString("0.00"), GraphicsDevice.Viewport.Width - 5, 5, Color.White, 2)
        ' Jumps
        DrawWord("Jumps : " + Me.Jumps.ToString, CInt(GraphicsDevice.Viewport.Width / 2.0), 5, Color.White, 1)
        spriteBatch.End()
        RestoreRenderState(States, GraphicsDevice)
        MyBase.Draw(gameTime)
    End Sub
    Public Sub ResetTime()
        mtmrStartTime = Nothing
        mtmrStartTime = New TimeSpan
    End Sub
    ' //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    ' Draws information where specified Justify 0 - Left, 1 - Centre, 2 - Right
    Private Sub DrawWord(ByVal strItem As String, ByVal intX As Integer, ByVal intY As Integer, ByVal colour As Color, Optional ByVal Justify As Integer = 0)
        Dim location As Vector2 = New Vector2(intX, intY)
        If (Justify > 0) Then
            Dim Size As Vector2 = mFont.MeasureString(strItem)
            If (Justify = 1) Then
                location.X -= Size.X / 2
            Else
                location.X -= Size.X
            End If
        End If
        spriteBatch.DrawString(mFont, strItem, New Vector2(location.X + 2, location.Y + 2), New Color(0, 0, 0, 150))
        spriteBatch.DrawString(mFont, strItem, location, colour)

    End Sub
    Protected Overrides Sub LoadContent()
        spriteBatch = New SpriteBatch(GraphicsDevice)
        bmpRight = Game.Content.Load(Of Texture2D)("rbutton")
        bmpNothing = Game.Content.Load(Of Texture2D)("nothing")
        bmpLeft = Game.Content.Load(Of Texture2D)("lbutton")
        bmpJump = Game.Content.Load(Of Texture2D)("jump")
        mFont = Game.Content.Load(Of SpriteFont)("motorwerk")
        MyBase.LoadContent()
    End Sub
    ''' <summary>
    ''' Allows the game component to update itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Public Overrides Sub Update(ByVal gameTime As GameTime)
        Dim touchCollection As TouchCollection = TouchPanel.GetState()
        Dim keyState As KeyboardState = Keyboard.GetState()
        Dim blnInLeft As Boolean = False

        mtmrGameTime = gameTime

        For Each TouchLocation In touchCollection
            If (IsTouching(TouchLocation, mLRectangle)) Then
                RaiseEvent ClickedItem(1)
                blnInLeft = True
            End If
            If (IsTouching(TouchLocation, mRRectangle) And Not blnInLeft) Then
                RaiseEvent ClickedItem(2)
            End If
            If (IsTouching(TouchLocation, mJumpRectangle)) Then
                RaiseEvent ClickedItem(3)
            End If
        Next

        If (keyState.IsKeyDown(Keys.Left)) Then
            RaiseEvent ClickedItem(1)
        End If
        If (keyState.IsKeyDown(Keys.Right)) Then
            RaiseEvent ClickedItem(2)
        End If
        If (keyState.IsKeyDown(Keys.Space)) Then
            RaiseEvent ClickedItem(3)
        End If
        ' Frame Rate Calculation
        If (mblnDisplayFrameRate) Then
            mtsElapsedTime += gameTime.ElapsedGameTime
            If (mtsElapsedTime > TimeSpan.FromSeconds(1)) Then
                mtsElapsedTime -= TimeSpan.FromSeconds(1)
                mintFrameRate = mintFrameCounter
                mintFrameCounter = 0
            End If
        End If

        ' TODO: Add your update code here
        MyBase.Update(gameTime)
    End Sub
    Private Function IsTouching(ByRef TouchLocation As TouchLocation, ByRef rect As Rectangle) As Boolean
        Return rect.Contains(New Point(CInt(TouchLocation.Position.X), CInt(TouchLocation.Position.Y)))
    End Function
End Class
